package com.gxar.quick.ar.server.base.model.works.mapper;

import com.gxar.quick.ar.server.base.common.enums.UserWorksSourceEnum;
import com.gxar.quick.ar.server.base.model.works.dto.WorksListDTO;
import com.gxar.quick.ar.server.base.model.works.vo.request.WorksPublishRequest;
import com.gxar.quick.ar.server.base.model.works.vo.response.*;
import com.gxar.quick.ar.server.db.model.WorksEntity;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Objects;

/**
 * 作品草稿转换工具
 *
 * @author Linsy
 * @version 1.0
 * @created 2023/11/8 10:45 AM
 */
@Mapper
public interface WorksMapStruct {
    WorksMapStruct INSTANCE = Mappers.getMapper(WorksMapStruct.class);

    /**
     * 作品业务实体转列表
     *
     * @param entity 实体
     * @return 响应
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getWorksId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    @Mapping(target = "source", expression = "java(getSourceNameByCode(entity.getSource()))")
    WorksListResponse listDtoToListResponse(WorksListDTO entity);

    /**
     * 作品业务实体转列表
     *
     * @param entities 实体
     * @return 响应
     */
    List<WorksListResponse> listDtoToListResponses(List<WorksListDTO> entities);

    /**
     * 实体转详情
     *
     * @param entity 作品实体
     * @return 详情
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    @Mapping(target = "userId", expression = "java(String.valueOf(entity.getUserId()))")
    @Mapping(target = "templateId", expression = "java(String.valueOf(entity.getTemplateId()))")
    WorksInfoResponse entityToInfoResponse(WorksEntity entity);

    /**
     * 实体转换更新响应
     *
     * @param entity 实体
     * @return 更新响应
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    @Mapping(target = "userId", expression = "java(String.valueOf(entity.getUserId()))")
    @Mapping(target = "templateId", expression = "java(String.valueOf(entity.getTemplateId()))")
    WorksUpdateResponse entityToUpdateResponse(WorksEntity entity);

    /**
     * 发布作品请求转作品
     *
     * @param worksPublishRequest 发布作品请求
     * @return 响应
     */
    @Mapping(target = "templateId", expression = "java(org.apache.commons.lang3.StringUtils.isNotBlank(worksPublishRequest.getTemplateId()) ? Long.parseLong(worksPublishRequest.getTemplateId()) : null)")
    WorksEntity publishToEntity(WorksPublishRequest worksPublishRequest);

    /**
     * 实体转发布作品响应
     *
     * @param entity 实体
     * @return 响应
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    @Mapping(target = "templateId", expression = "java(String.valueOf(entity.getTemplateId()))")
    WorksPublishResponse entityToPublish(WorksEntity entity);

    /**
     * 实体转分享详情响应
     *
     * @param entity 实体
     * @return 分享详情响应
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    @Mapping(target = "userId", expression = "java(String.valueOf(entity.getUserId()))")
    @Mapping(target = "templateId", expression = "java(String.valueOf(entity.getTemplateId()))")
    WorksShareInfoResponse entityToShareInfoResponse(WorksEntity entity);

    /**
     * 作品业务实体转列表
     *
     * @param entity 实体
     * @return 响应
     */
    @Mapping(target = "worksId", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "slotId", expression = "java(String.valueOf(entity.getSlotId()))")
    WorksListResponse entityToListResponse(WorksEntity entity);

    /**
     * 作品业务实体转列表
     *
     * @param entities 实体
     * @return 响应
     */
    List<WorksListResponse> entitiesToListResponses(List<WorksEntity> entities);

    /**
     * 根据资源code查询
     *
     * @param sourceCode 资源code
     * @return 响应
     */
    default String getSourceNameByCode(Integer sourceCode) {
        UserWorksSourceEnum worksSourceEnum = UserWorksSourceEnum.getByCode(sourceCode);
        if (Objects.nonNull(worksSourceEnum)) {
            return worksSourceEnum.getName();
        }
        return null;
    }

}
